package com.ruoyi.project.opencv.test;

public class Uni {
public static void main(String[] args) {
        Uni rc4 = new Uni();

        //String plaintext = "4e2d65870065006e0067006c006900730068";
        //String plaintext = "����English";
        //String ciphertext = rc4.convert(plaintext);
        //System.out.print("����Ϊ��" + plaintext + "\n" + "����Ϊ��" + ciphertext + "\n" );
        /*String plaintext = "\u4f60\u597d";
        String ciphertext = rc4.revert(plaintext);
        System.out.print("����Ϊ��" + plaintext + "\n" + "����Ϊ��" + ciphertext + "\n" );*/
    }
    public String revert(String str)     { 
       str = (str == null ? "" : str); 
       if (str.indexOf("\\u") == -1)//�������unicode����ԭ������ 
       return str; 
       StringBuffer sb = new StringBuffer(1000); 
       for (int i = 0; i < str.length() - 6;)  { 
       String strTemp = str.substring(i, i + 6); 
       String value = strTemp.substring(2); 
       int c = 0; 
       for (int j = 0; j < value.length(); j++) { 
         char tempChar = value.charAt(j); 
         int t = 0; 
           switch (tempChar) { 
             case 'a': 
             t = 10; 
             break; 
             case 'b': 
             t = 11; 
             break; 
             case 'c': 
             t = 12; 
             break; 
             case 'd': 
             t = 13; 
             break; 
             case 'e': 
             t = 14; 
             break; 
             case 'f': 
             t = 15; 
             break; 
             default: 
             t = tempChar - 48; 
             break; 
           } 
           c += t * ((int) Math.pow(16, (value.length() - j - 1))); 
         } 
         sb.append((char) c); 
         i = i + 6; 
       } 
       return sb.toString(); 
    }

   /** 
    * ���ַ���ת��unicode 
    * @param str ��ת�ַ��� 
    * @return unicode�ַ��� 
    */ 
    public String convert(String str)  { 
      str = (str == null ? "" : str); 
      String tmp; 
      StringBuffer sb = new StringBuffer(1000); 
      char c; 
      int i, j; 
      sb.setLength(0); 
      for (i = 0; i < str.length(); i++) 
      { 
      c = str.charAt(i); 
      sb.append("\\u"); 
      j = (c >>>8); //ȡ����8λ 
      tmp = Integer.toHexString(j); 
      if (tmp.length() == 1) 
      sb.append("0"); 
      sb.append(tmp); 
      j = (c & 0xFF); //ȡ����8λ 
      tmp = Integer.toHexString(j); 
      if (tmp.length() == 1) 
      sb.append("0"); 
      sb.append(tmp); 

      } 
      return (new String(sb)); 
    }
    public static String string2Unicode(String string) {
 
       StringBuffer unicode = new StringBuffer(); 
       for (int i = 0; i < string.length(); i++) { 
        char c = string.charAt(i);
        unicode.append(Integer.toHexString(c));
       } 
       return unicode.toString();
   }
   public static String unicode2String(String unicode) { 
    StringBuffer string = new StringBuffer(); 
    String[] hex = unicode.split("\\\\u");  
      for (int i = 1; i < hex.length; i++) {   
          // ת����ÿһ�������
          int data = Integer.parseInt(hex[i], 16);   
          // ׷�ӳ�string
          string.append((char) data);
      } 
      return string.toString();
   }
}